package com.googlecode.perfectable.eventable.routing;

import java.util.Deque;
import java.util.Iterator;
import java.util.concurrent.LinkedBlockingDeque;

import com.google.common.collect.Iterators;

public interface Source<M> {
	void listen(Sink<M> sink);

	void stop(Sink<M> sink);

	default void weakListen(Sink<M> sink) {
		WeakSinkWrapper.attach(this, sink);
	}

	default Iterator<M> blockingIterator() {
		final Deque<M> collected = new LinkedBlockingDeque<>();
		weakListen((m) -> collected.add(m));
		return Iterators.consumingIterator(collected.iterator());

	}

}
